package com.jichangxiu.framework.aspect;

import com.jichangxiu.common.annotation.Log;
import com.jichangxiu.common.context.JcxContext;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.framework.utils.LogRuleUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(log)")
    public void pointcutLogAround(Log log) {
    }

    @Around(value = "pointcutLogAround(log)", argNames = "proceedingJoinPoint , log")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint, Log log) throws Throwable {
        long start = System.currentTimeMillis();
        // 已经登录的请求才会被记录
        if (log.enabled() && StrUtils.isNotEmpty(JcxContext.getJti()))
            return LogRuleUtils.recordRequestLog(proceedingJoinPoint, log, start);
        return proceedingJoinPoint.proceed();
    }

}
